import 'package:flutter/material.dart';
import '../../api/api_service.dart';
import '../../provide/userProvide.dart';
import '../../routers/application.dart';
import 'package:provide/provide.dart';

class MyOrderPage extends StatefulWidget {
  @override
  _MyOrderPageState createState() => _MyOrderPageState();
}

class _MyOrderPageState extends State<MyOrderPage> {
  List orderList = [];

  @override
  void initState() {
    Future.delayed(Duration.zero, () {
      this._getMyOrder();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Color.fromRGBO(244, 245, 245, 1.0),
        appBar: AppBar(
          title: Text('我的订单'),
        ),
        body: Container(
            decoration: BoxDecoration(
                border:
                    Border(right: BorderSide(width: 1, color: Colors.black12))),
            child: ListView.builder(
              itemCount: orderList.length,
              itemBuilder: (context, index) {
                return _leftInkWel(index);
              },
            )));
  }

  Widget _leftInkWel(int index) {
    return InkWell(
        child: Card(
      margin: EdgeInsets.all(10),
      child: Column(
        children: <Widget>[
          ListTile(
            title:
                Text(orderList[index]['name'], style: TextStyle(fontSize: 28)),
            subtitle: Text("编号"),
          ),
          Divider(),
          ListTile(
            title: Text("价格     ${orderList[index]['money']}/小时"),
          ),
          ListTile(
            title: Text(
                "状态:   ${orderList[index]['state'] == true ? '正常' : '异常'}"),
          ),
          FlatButton(
            color: Colors.pink,
            child: Text(
              orderList[index]['end_data'] == null ? '还车' : '删除订单',
              style:
                  TextStyle(color: Colors.white, backgroundColor: Colors.pink),
            ),
            onPressed: () {
              if (orderList[index]['end_data'] == null) {
                _updateRent(orderList[index]['_id']);
              } else {
                _deleteRent(orderList[index]['_id']);
              }
            },
          )
        ],
      ),
    ));
  }

  _getMyOrder() async {
    try {
      var userInfo = Provide.value<UserProvide>(context).userIofo;
      await myOrderApi(userInfo.id).then((data) async {
        if (data['success']) {
          setState(() {
            orderList = data['result'];
          });
        } else {
          showDialog(
              context: context,
              builder: (_) => new AlertDialog(content: new Text(data['msg'])));
        }
      });
    } catch (e) {
      print('登录异常');
      print(e);
    }
  }

  // 还车
  _updateRent(String id) async {
    try {
      await updateRentApi(id).then((data) async {
        if (data['success']) {
          showDialog(
              context: context,
              builder: (_) => new AlertDialog(content: new Text(data['msg'])));
          _getMyOrder();
        } else {
          showDialog(
              context: context,
              builder: (_) => new AlertDialog(content: new Text(data['msg'])));
        }
      });
    } catch (e) {
      print('登录异常');
      print(e);
    }
  }

  _deleteRent(String id) async {
    try {
      await deleteRentApi(id).then((data) async {
        if (data['success']) {
          showDialog(
              context: context,
              builder: (_) => new AlertDialog(content: new Text(data['msg'])));
          _getMyOrder();
        } else {
          showDialog(
              context: context,
              builder: (_) => new AlertDialog(content: new Text(data['msg'])));
        }
      });
    } catch (e) {
      print('登录异常');
      print(e);
    }
  }
}
